package com.zhkj.shixi.tvplayer.PicturePlayer;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.zhkj.shixi.tvplayer.R;
import com.zhkj.shixi.tvplayer.banner.Banner;
import com.zhkj.shixi.tvplayer.banner.BannerConfig;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.content.ContentValues.TAG;

/**
 * Created by nieshuang on 19-8-8.
 */

public class PicPlayerUI implements View.OnClickListener,ViewPager.OnPageChangeListener,SeekBar.OnSeekBarChangeListener{
    private Activity mPicActivity;
    private ViewGroup mToolsLayout;
    private ViewGroup mInfoLayout;
    private ImageButton mBtnPlay;
    private ImageButton mBtnPrev;
    private ImageButton mBtnNext;
    private ImageButton mBtnTurn;
    private ImageButton mBtnBgMusic;
    private SeekBar mTimeseekbar;
    private Banner mBanner;
    private int mCurrentIndex;
    private int mPosition;
    private int mTimePosition;//轮播间隔
    private Timer mTimer;
    private TextView mPicName;
    private TextView mPicStyle;
    private TextView mPicSize;
    private TextView mPicTime;
    private PicInfo picInfo;
    private TextView mTime;
    private View mPlayListShow;
    private ListView mImageAudioList;

    private Boolean status = true;
    private boolean isBtnPlayOff = false;

    public Banner getBanner() {
        return mBanner;
    }

    public SeekBar getSeekBar(){
        return mTimeseekbar;
    }
    public View getmPlayListShow(){
        return mPlayListShow;
    }

    PicPlayerUI(Activity activity) {
        mPicActivity = activity;
        mTimer = new Timer();
        init();
        setfocus();
        hideToolsLayout();
    }

    private void init() {
        mBtnPrev = mPicActivity.findViewById(R.id.pre_normal_button);
        mBtnPlay = mPicActivity.findViewById(R.id.play_button);
        mBtnNext = mPicActivity.findViewById(R.id.next_normal_button);
        mBtnTurn = mPicActivity.findViewById(R.id.player_turn);
        mBtnBgMusic = mPicActivity.findViewById(R.id.player_music_normal);
        mToolsLayout = mPicActivity.findViewById(R.id.pic_tools_ctrl);
        mInfoLayout = mPicActivity.findViewById(R.id.info_layout);
        mTimeseekbar =mPicActivity.findViewById(R.id.player_speed_bar);
        mBanner =mPicActivity.findViewById(R.id.pic_view);
        mTime =mPicActivity.findViewById(R.id.seconds_description_text);
        mTimePosition =mTimeseekbar.getProgress();
        mTime.setText(mTimePosition+"s");
        mTimeseekbar.setEnabled(false);
        picInfo = new PicInfo(mPicActivity);

        mPicName = mPicActivity.findViewById(R.id.text_pic_name);
        mPicStyle = mPicActivity.findViewById(R.id.text_pic_type);
        mPicSize = mPicActivity.findViewById(R.id.text_pic_size);
        mPicTime = mPicActivity.findViewById(R.id.text_pic_time);
        mPlayListShow = mPicActivity.findViewById(R.id.image_play_listshow);
        mImageAudioList =mPicActivity.findViewById(R.id.img_audiolist);

        //面板隐藏
        mToolsLayout.setVisibility(View.GONE);
        mInfoLayout.setVisibility(View.GONE);
        initBanner();
        //初始化按钮监听
        mBtnNext.setOnClickListener(this);
        mBtnPrev.setOnClickListener(this);
        mBtnPlay.setOnClickListener(this);
        mBtnTurn.setOnClickListener(this);
        mBtnBgMusic.setOnClickListener(this);
        mBanner.setOnPageChangeListener(this);
        mTimeseekbar.setOnSeekBarChangeListener(this);
    }

    // 监听事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_button:
                //播放与暂停
                if (status){
                    if (isBtnPlayOff){
                        mBtnPlay.setImageResource(R.drawable.
                                player_video_button_play_normal_selector);
                        isBtnPlayOff = false;
                        mBanner.stopAutoPlay();

                    }else {
                        mBtnPlay.setImageResource(R.drawable.
                                player_video_button_suspend_normal_selector);
                        isBtnPlayOff = true;

                        mBanner.startAutoPlay();
                    }
                }
                break;

            case R.id.player_music_normal:
                //播放音乐
                Log.i(TAG, "onClick:mmmmmmmmmmmmm "+((PicPlayActivity)mPicActivity).isOver());

                if(((PicPlayActivity)mPicActivity).isOver()){
                    mPlayListShow.setVisibility(View.VISIBLE);
                }
                else {
                    Toast.makeText(mPicActivity, "音乐列表未加载完", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.player_turn:
                //轮播模式
                if (status){
                    mBtnTurn.setImageResource(R.drawable.player_img_off_selector);
                }else {
                    mBtnTurn.setImageResource(R.drawable.player_img_on_selector);
                }
                status=!status;
                break;

            case R.id.next_normal_button:
                displayTools();
                nextPic();
//                refreshPicInfo();//刷新图片信息

                Log.i(TAG, "mtime"+mTimePosition);
                Log.i(TAG, "mtimes: "+mTimeseekbar.getProgress());

                break;
            case R.id.pre_normal_button:
                displayTools();
                //播放上一张图片
                lastPic();

//                refreshPicInfo();//刷新图片信息
                break;
        }
    }

    public void refreshPicInfo(){
        mPicName.setText(picInfo.getPicName());
        mPicStyle.setText(picInfo.getPicType());
        mPicSize.setText(picInfo.getPicSize());
        mPicTime.setText(picInfo.getPicDate());
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.e(TAG, "xxxxxxxxx: ");
        mPosition =position+1;
        refreshPicInfo();//刷新图片信息

    }

    @Override
    public void onPageSelected(int position) {
        mCurrentIndex =position;
        mBanner.setRotation(0);
        Log.i(TAG, "xxxxxx: "+mCurrentIndex);
        refreshPicInfo();//刷新图片信息
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        Log.i(TAG, "xxxxxxxxxxxxxx: ");
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mTimePosition =progress+1;
        displayTools();
        mTime.setText(mTimePosition+"s");
        mBanner.setDelayTime(mTimePosition*1000);
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    /**
     * 显示面板
     */
    public void displayTools() {
        mToolsLayout.setVisibility(View.VISIBLE);
        mInfoLayout.setVisibility(View.VISIBLE);
        hideToolsLayout();
    }

    /**
     * 让控制面板获得焦点
     */
    public void setfocus(){
        mToolsLayout.requestFocus();
    }
    public Boolean isSeekBarfocus(){
        return mTimeseekbar.hasFocus();
    }

    /**
     * 面板隐藏状态
     * @return
     */
    public void hideToolsLayout() {
        mTimer.cancel();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mPicActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mToolsLayout.setVisibility(View.GONE);
                        mInfoLayout.setVisibility(View.GONE);
                        mBanner.setScrollables(true);
                    }
                });
            }
        },8000);

    }

    /**
     * 判断面板状态
     * @return
     */

    public Boolean isCtlVisiable(){
        if (mToolsLayout.getVisibility()==View.VISIBLE){
            return true;
        }else
            return false;
    }
    /**
     * 切换下一张图片
     */
    public void nextPic(){
        mBanner.setItem(mPosition+1);
//        Log.i(TAG, "nextPic: "+mCurrentIndex);
    }

    /**
     * 上一张图片
     */
    public void lastPic(){
        mBanner.setItem(mPosition-1);
    }

    /**
     * 修改轮播间隔
     */
    public void seekBarUp(){
        mTimePosition=mTimePosition+1;
        mTimeseekbar.setProgress(mTimePosition);
    }
    public void seekBarDown(){
        mTimePosition=mTimePosition-1;
        mTimeseekbar.setProgress(mTimePosition);
    }

    /**
     * mBanner初始化
     */
    public void initBanner(){
        mBanner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        mBanner.setImageLoader(new GlideImageLoader());
        List<String> list = ((PicPlayActivity)mPicActivity).getPathList();

        mBanner.setImages(list);
//        mBanner.setImages(((PicPlayActivity)mPicActivity).getPicList());
        mBanner.setDelayTime(mTimePosition*1000);
        mBanner.isAutoPlay(true);
        mBanner.setScaleType(2);
        mBanner.start();
        mBanner.stopAutoPlay();
        mBanner.setItemStart(((PicPlayActivity)mPicActivity).getPicList().indexOf(((PicPlayActivity)mPicActivity).getStartFile())+1);
        Log.i(TAG, "xxxxx: "+((PicPlayActivity)mPicActivity).getPicList().indexOf(((PicPlayActivity)mPicActivity).getStartFile()));
        mCurrentIndex =((PicPlayActivity)mPicActivity).getPicList().indexOf(((PicPlayActivity)mPicActivity).getStartFile());


    }

    public int getmCurrentIndex() {
        return mCurrentIndex;
    }

    public ListView getmImageAudioList() {
        return mImageAudioList;
    }
    public Boolean getIsPlaying(){
        return isBtnPlayOff;
    }
}